package com.bsj.power.system.web;

import com.alibaba.excel.EasyExcelFactory;
import com.bsj.power.common.config.exception.ApiException;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.config.global.JsonResultEnum;
import com.bsj.power.common.def.dto.PortalLoginLogSearchDTO;
import com.bsj.power.common.def.vo.pls.excel.CardReportExcelVO;
import com.bsj.power.common.def.vo.sys.PortalLoginLogVO;
import com.bsj.power.common.def.vo.sys.excel.PortalLoginLogExcelVO;
import com.bsj.power.common.util.DateUtils;
import com.bsj.power.system.service.PortalLoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description PortalLoginLogController
 * @time 2024/10/15 15:14
 */
@RestController
@RequestMapping("/system/portalLoginLog")
public class PortalLoginLogController {

    @Autowired
    private PortalLoginLogService portalLoginLogService;


    /**
     * 门户单点登录日志
     *
     * @param portalLoginLogSearchDTO
     * @return com.bsj.power.common.config.global.JsonResult<java.util.List < com.bsj.power.common.def.vo.sys.PortalLoginLogVO>>
     * @author ljx
     * @time 2024/10/15 15:20
     */
    @GetMapping("/page")
    public JsonResult<List<PortalLoginLogVO>> page(@Validated PortalLoginLogSearchDTO portalLoginLogSearchDTO) {
        return portalLoginLogService.page(portalLoginLogSearchDTO);
    }

    /**
     * 单点登录记录导出
     *
     * @param response
     * @param portalLoginLogSearchDTO
     * @return void
     * @author ljx
     * @time 2024/10/15 15:50
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response, PortalLoginLogSearchDTO portalLoginLogSearchDTO) throws IOException {
        List<PortalLoginLogExcelVO> portalLoginLogExcelVOS = portalLoginLogService.getExportData(portalLoginLogSearchDTO);
        if (CollectionUtils.isEmpty(portalLoginLogExcelVOS)) {
            throw new ApiException(JsonResultEnum.NO_EXPORT_DATA);
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //编码 防止中文乱码
        String fileName = URLEncoder.encode("单点登录记录" + DateUtils.dateToStr(DateUtils.TIME_STANDARD_FORMAT2, new Date()), "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        EasyExcelFactory.write(response.getOutputStream(), PortalLoginLogExcelVO.class).sheet("单点登录记录")
                .doWrite(portalLoginLogExcelVOS);
    }
}
